package com.mindfreez.shipping.usps;

import com.mindfreez.shipping.Address;
import com.mindfreez.shipping.Container;
import com.mindfreez.shipping.Rate;
import com.mindfreez.shipping.usps.domain.RateRequest;
import com.mindfreez.shipping.usps.domain.Service;
import com.thoughtworks.xstream.XStream;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

public class RateService {
    private static final Logger log = Logger.getLogger(RateService.class);

    private String url, userId;

    public RateService(String url, String userId) {
        this.url = url;
        this.userId = userId;
    }

    List<Rate> findRates(Address shipFrom, Address shipTo, Container container, Service service) {
        XStream xStream = new XStream();
        xStream.processAnnotations(new Class[]{
                RateRequest.class,
                com.mindfreez.shipping.usps.domain.Address.class,
                com.mindfreez.shipping.usps.domain.Service.class });

        RateRequest request = new RateRequest();
        request.setUserId(userId);

        request.setRevision(2);

        com.mindfreez.shipping.usps.domain.Container uspsContainer = new com.mindfreez.shipping.usps.domain.Container();

        uspsContainer.setId("1ST");
        uspsContainer.setMachinable("true");
        uspsContainer.setPounds(container.getPounds().toString());
        uspsContainer.setOunces(container.getOunces().toString());
        uspsContainer.setFirstClassMailType("LETTER");
        uspsContainer.setService(service.getName());
        uspsContainer.setZipDestination(shipTo.getPostalCode());
        uspsContainer.setZipOrigination(shipFrom.getPostalCode());

        uspsContainer.setContainer("");
        uspsContainer.setSize("REGULAR");

        request.getContainers().add(uspsContainer);

        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("API", "RateV4"));

String xml = xStream.toXML(request);
log.debug("XML: ["+xml +"]");

        params.add(new BasicNameValuePair("XML", xml));

        List<Rate> rate = null;
        
        try {
//            URI uri = URIUtils.createURI("http", "testing.shippingapis.com", -1, "/ShippingAPI.dll", URLEncodedUtils.format(params, "UTF-8"), null);
            URI uri = URIUtils.createURI("http", "production.shippingapis.com", -1, "/ShippingAPI.dll", URLEncodedUtils.format(params, "UTF-8"), null);
            HttpClient httpclient = new DefaultHttpClient();

            HttpGet get = new HttpGet(uri);

            log.debug(get.getURI());

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String response = httpclient.execute(get, responseHandler);

            log.debug(response);
        } catch (IOException ex) {
            log.error(ex);
        } catch (URISyntaxException ex) {
            log.error(ex);
        }
        
        return rate;
    }
}
